﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoPro.Local;
using System.Xml.Linq;
using System.Web;
using System.Diagnostics;

namespace CoPro.Provisioning.ContentProvider
{
    public class XMLContentProvider : IContentProvider
    {
        private String dataPath;
        private XDocument dataSource;

        public XMLContentProvider()
        {

        }

        public string GetContent(string _key, ILocalProvider _localProvider)
        {
            IContentLocal local = _localProvider.GetLocal();
            try
            {
                //get the content element
                XElement contentElement = dataSource.Element("contents").Elements().Where(e => e.Name == "content" && e.Attribute("key").Value == _key).Single();

                //Find the correct language tag
                XElement languageElement = null;

                while (languageElement == null && local != null)
                {
                    var languageElementQueryResult = contentElement.Elements().Where(e => e.Name == "local" && e.Attribute("name").Value == local.LocalAbbreviation);
                    int resultCount = languageElementQueryResult.Count();

                    switch (resultCount)
                    {
                        //no element for the current language, try next local
                        case 0:
                            local = _localProvider.GetNextLocal(local);
                            break;
                        //found element, so go on and work with this
                        case 1:
                            languageElement = languageElementQueryResult.Single();
                            break;
                        default:
                            Trace.TraceError("Ambigious language reference for local: " + local.LocalAbbreviation);
                            throw new InvalidOperationException("Ambigious language reference for local: " + local.LocalAbbreviation);
                    }
                }

                //check if language is null, if it is throw an exception
                if (languageElement == null)
                {
                    Trace.TraceError("No content element found for key: " + _key);
                    throw new InvalidOperationException("No content element found.");
                }

                //create the output string
                String retString = "";

                foreach (XElement element in languageElement.Elements())
                {
                    retString += element.ToString();
                }

                return retString;
            }
            catch (InvalidOperationException e)
            {
                //this exception catches the case if no or an ambigious content element was found. 
                Trace.TraceError("Failed to retrieve content element. Message was: " + e.Message);
                //TODO what should we do if the key was not found? 
            }
            return "";
        }

        public void Initialize(string _configString)
        {
            dataPath = _configString;
            LoadDataSource();
        }

        private void LoadDataSource()
        {
            string xmlPath = HttpContext.Current.Server.MapPath(dataPath);
            dataSource = XDocument.Load(xmlPath);
        }



    }
}
